

app.controller(
				'InfoSetCtrl',
				[
						'$scope',
						'$http',
						'dialogs',
						'$state',
						'$uibModal',
						'InfoSetService',
						function($scope, $http, dialogs, $state, $uibModal,
								InfoSetService) {
							var vm = $scope.vm = this;
							var defaultQuery = {
								searchType : "name"
							};
							vm.query = angular.copy(defaultQuery);
							vm.addinfoTypeShow = false;
							vm.infoTypeFlexigridObj = {};
							vm.options = InfoSetService.gridOptions();

							// 消息类型树
							vm.treeObj = {};
							vm.treeSetting = {
								async : {
									url : ISS.getPath('/infoType/tree'),
									enable : true,
									dataType : "json",
									autoParam : [ "id" ]
								},
								data : {
									simpleData : {
										enable : true
									}
								},
								edit : {
									enable : true,
									removeTitle : getMessage("common.del"),
									renameTitle : "编辑",
									showRenameBtn : function (treeId, treeNode) {
										if(treeNode.level==0){
											return false;
										}
										return true;
									},
									showRemoveBtn : function (treeId, treeNode) {
										if(treeNode.level==0){
											return false;
										}
										return true;
									}
								},
								callback : {
									beforeEditName: false,
									onClick : function(event, treeId, treeNode,
											clickFlag) {
										//判断是否显示添加消息类型按钮
										if(treeNode.level==0){
											vm.addinfoTypeShow = true;
										}else{
											vm.addinfoTypeShow = false;
										}
										
										//在右侧表格中显示当前节点下的角色信息
										vm.infoTypeFlexigridObj.flexOptions({
											newp : 1,
											extParam : {"infoTypeId":treeNode.id}
										}).flexReload();
										
										vm.treeData = treeNode;
										setParam(treeNode);
									},
									beforeRemove : function(treeId, treeNode) {
										vm.treeData = treeNode;
										if (Number(treeNode.attrMap.dataCount)) {
											dialogs
													.notify(
															undefined,
															getMessage("common.deleteUserGroupTip"));
										} else {
											zTreeOnRemove(treeNode);
										}

										return false;
									},
									beforeEditName : function(treeId, treeNode,
											newName, isCancel) {
										vm.treeData = treeNode;
										zTreeOnRename(treeNode);
										return false;
						
									}
								}
							};
							
							// 重新异步加载子节点，新增或修改的时候
				            var reAsyncChildNodes = function (node) {
				            	var parentNode;
				            	if(!node){
				            		parentNode = param;
				            	}else{
				            		//获取选中节点的父节点,用于刷新子节点
				                    parentNode = node.getParentNode();
				            	}
				                vm.treeObj.reAsyncChildNodes(parentNode, "refresh");
				            };
				            
							// 删除节点
							var zTreeOnRemove = function(data) {
								if (ISS.isNotEmpty(data.id)) {
									// 弹出确认删除提示信息
									var dlg = dialogs.confirm(undefined,
											getMessage("common.deleteConfirm"));
									dlg.result.then(function(param) {
										InfoSetService.delInfoType(data.id).then(function(data) {
														// 刷新左侧树
											vm.treeObj.reAsyncChildNodes(data.id, "refresh");
													});
											});
								}
							};
							
							// 修改节点
							var zTreeOnRename = function(data) {
								if (ISS.isNotEmpty(data.id)) {
									vm.openAddInfoType('editInfoType');
								} else {
									alert(data.name);
									// 提示请选择一条或多条信息
									dialogs
											.notify(
													undefined,
													getMessage("common.plzSelectOneRecourd"));
								}
							};

							
							vm.openAddInfoType = function(index) {
								var param = {"treeNode":vm.treeData,
										"flexiGrid":vm.infoTypeFlexigridObj};
								if(ISS.isEmpty(param)){
									dialogs.notify(undefined,
												getMessage("infoset.selectinfotype"));
									return;
								  }
								var modalInstance = $uibModal.open({
									animation : true,
									templateUrl : index,
									controller : index+'Ctrl',
									size : 'lg',
									resolve : {
										param : function() {
											return param;
										}
									}
								});
								modalInstance.result.then(function(result) {
									var result = result;
								});
							}

							// 设置查询参数
							var setParam = function(data) {
								vm.selectedId = data.id;
								vm.selectedName = data.name;
								// 点击建筑树节点时，查询该建筑节点下一级的建筑（不包括该节点）
								vm.infoTypeFlexigridObj.flexOptions({
									newp : 1,
									extParam : {
										'parentId' : data.id
									}
								}).flexReload();
							}
							
							// 给消息类型添加角色
							vm.addRoles = function(addRoles) {
								vm.openAddInfoType(addRoles);
							}

							// 从消息类型中移出角色
							vm.removeRole = function(){
								var selectData = vm.infoTypeFlexigridObj.getCheckedRows();
								  if(ISS.isEmpty(selectData)){
									// 提示请选择一条或多条信息
									dialogs.notify(undefined,
												getMessage("common.plzSelectOneRecourd")); 
									return;
								  }
								  var params = {
										  "roleIds" : [],
										  "infoTypeId" : vm.treeData.id
									};
								  angular.forEach(selectData, function(data) {
									  params.roleIds.push(data.id);
									});
								  var dlg = dialogs.confirm(getMessage("infoset.delRolestoInfoType"));
								  dlg.result.then(function(result){					
									  InfoSetService.removeRole(params).then(function(data){
										  vm.infoTypeFlexigridObj.flexOptions({
												newp : 1,
												extParam : {"infoTypeId":vm.treeData.id}
											}).flexReload();
										});
									});
							}
							
							vm.addAndDelRoles = function(){
								if(!vm.treeData){
									return false;
								}else{
									if(vm.treeData.level == 1){
										return true;
									}else{
										return false;
									}
								}
							}
							
							
						} ]);

app.controller('addInfoTypeCtrl', [
		'$scope',
		'$http',
		'$state',
		'dialogs',
		'$uibModalInstance',
		'param',
		'InfoSetService',
		function($scope, $http, $state, dialogs, $uibModalInstance, param,
				InfoSetService) {
			var vm = $scope.vm = this;
			var treeObj =  $.fn.zTree.getZTreeObj("infoType");
			vm.parentId = 0;
			vm.category = param.treeNode.name;

			vm.infoType = {
				level : param.treeNode.level + 1,
				infoTypeParentCode : param.treeNode.id
			}

			vm.close = function() {
				$uibModalInstance.close();
			};

			vm.saveInfoType = function() {
				InfoSetService.addInfoType(vm.infoType).then(function(data) {
					if (data.success === false) {
						dialogs.notify(data);
					} else {
						dialogs.notify(data).closed.then(function() {
							vm.close();
							treeObj.reAsyncChildNodes(param.treeNode, "refresh");
						});
					}
				});
			}
			
		} ]);

app.controller('editInfoTypeCtrl', [
		'$scope',
		'$http',
		'$state',
		'dialogs',
		'$uibModalInstance',
		'param',
		'InfoSetService',
		function($scope, $http, $state, dialogs, $uibModalInstance, param,
				InfoSetService) {
			var vm = $scope.vm = this;
			var treeObj =  $.fn.zTree.getZTreeObj("infoType");
			var node = treeObj.getNodeByParam("id",param.treeNode.pId,null);
			vm.parentId = 0;
			vm.category = node.name;
			vm.infoType = {
					infoTypeId:param.treeNode.id,
					infoTypeName:param.treeNode.name
			}
			InfoSetService.selectInfoType(vm.infoType).then(function(data){
				 vm.infoType = data;
			});
			
			vm.close = function() {
				$uibModalInstance.close();
			};

			vm.updateInfoType = function() {
				InfoSetService.updateInfoType(vm.infoType).then(function(data) {
					if (data.success === false) {
						dialogs.notify(data);
					} else {
						dialogs.notify(data).closed.then(function() {
							vm.close();
							treeObj.reAsyncChildNodes(param.treeNode.getParentNode(), "refresh");
						});
					}
				});
			}
			
			
		} ]);

app.controller('addRolesCtrl', [
		'$scope',
		'$http',
		'$state',
		'dialogs',
		'$uibModalInstance',
		'param',
		'InfoSetService',
		function($scope, $http, $state, dialogs, $uibModalInstance, param,
				InfoSetService) {
			var vm = $scope.vm = this;
			vm.treeObj =  $.fn.zTree.getZTreeObj("infoType");
			vm.infoTypeFlexigridObj = {};
			var infoTypeId = param.treeNode.id;
			vm.options = InfoSetService.gridOptions("/infoType/checkRoles/"+infoTypeId);
			vm.close = function() {
				$uibModalInstance.close();
			};
			
			if(ISS.isEmpty(param.treeNode)){
				dialogs.notify(undefined,
							getMessage("infoset.selectinfotype"));
				return;
			  }
			
			//新增用户入组(批量)
			vm.infoTypeAddRole = function(){
					  var selectData = vm.infoTypeFlexigridObj.getCheckedRows();
					  if(ISS.isEmpty(selectData)){
						// 提示请选择一条或多条信息
						dialogs.notify(undefined,
									getMessage("common.plzSelectOneRecourd")); 
						return;
					  }
					  
					  if(ISS.isEmpty(param.treeNode)){
						  var firm = dialogs.notify(undefined,
										getMessage("infoset.selectinfotype")); 
							return;
						  }
					  
					  var params = {
							  "roleIds" : [],
							  "infoTypeId" : param.treeNode.id
					  }
					  angular.forEach(selectData,function(data){
						  params.roleIds.push(data.id);
					  });
					  var dlg = dialogs.confirm(getMessage("infoset.addRolestoInfoType"));
					  dlg.result.then(function(result){					
						  InfoSetService.infoTypeAddRole(params).then(function(data){
							  vm.close();
							  param.flexiGrid.flexOptions({
									newp : 1,
									extParam : {"infoTypeId":param.treeNode.id}
								}).flexReload();
							});
						});
					 
			}
			
		} ]);
                            		